package com.fooddelivery.dao;

import com.fooddelivery.DatabaseConnection;
import com.fooddelivery.model.DeliveryPerson;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeliveryPersonDAO {

    // 获取所有派送员
    public List<DeliveryPerson> getAllDeliveryPersons() {
        List<DeliveryPerson> deliveryPersons = new ArrayList<>();
        String sql = "SELECT * FROM delivery_persons";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql);
             ResultSet rs = stmt.executeQuery()) {
            while (rs.next()) {
                DeliveryPerson deliveryPerson = new DeliveryPerson();
                deliveryPerson.setId(rs.getInt("id"));
                deliveryPerson.setName(rs.getString("name"));
                deliveryPerson.setLocation(rs.getString("location"));
                deliveryPerson.setStatus(rs.getString("status"));
                deliveryPersons.add(deliveryPerson);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return deliveryPersons;
    }

    // 获取空闲的派送员
    public List<DeliveryPerson> getAvailableDeliveryPersons() {
        List<DeliveryPerson> deliveryPersons = new ArrayList<>();
        String sql = "SELECT * FROM delivery_persons WHERE status = '空闲'";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql);
             ResultSet rs = stmt.executeQuery()) {
            while (rs.next()) {
                DeliveryPerson deliveryPerson = new DeliveryPerson();
                deliveryPerson.setId(rs.getInt("id"));
                deliveryPerson.setName(rs.getString("name"));
                deliveryPerson.setLocation(rs.getString("location"));
                deliveryPerson.setStatus(rs.getString("status"));
                deliveryPersons.add(deliveryPerson);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return deliveryPersons;
    }

    // 更新派送员状态
    public void updateDeliveryPersonStatus(int deliveryPersonId, String status) {
        String sql = "UPDATE delivery_persons SET status = ? WHERE id = ?";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, status);
            stmt.setInt(2, deliveryPersonId);
            stmt.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    // 添加派送员
    public void addDeliveryPerson(DeliveryPerson deliveryPerson) {
        String sql = "INSERT INTO delivery_persons (name, location, status) VALUES (?, ?, ?)";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, deliveryPerson.getName());
            stmt.setString(2, deliveryPerson.getLocation());
            stmt.setString(3, deliveryPerson.getStatus());
            stmt.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    // 根据 ID 获取派送员
    public DeliveryPerson getDeliveryPersonById(int id) {
        DeliveryPerson deliveryPerson = null;
        String sql = "SELECT * FROM delivery_persons WHERE id = ?";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    deliveryPerson = new DeliveryPerson();
                    deliveryPerson.setId(rs.getInt("id"));
                    deliveryPerson.setName(rs.getString("name"));
                    deliveryPerson.setLocation(rs.getString("location"));
                    deliveryPerson.setStatus(rs.getString("status"));
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return deliveryPerson;
    }

    // 删除派送员
    public void deleteDeliveryPerson(int id) {
        String sql = "DELETE FROM delivery_persons WHERE id = ?";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            stmt.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
